package com.my.mq.ms;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

@Service
public class MessageSender {

	@Autowired
	private JmsTemplate jmsQueueTemplate;
	
    /**
     * 指定队列发送消息
     */
    public void sendMessage(Destination destination, final String msg) {
      System.out.println("队列" + destination.toString() + "发送的消息------------" + msg);
      jmsQueueTemplate.send(destination, new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
          return session.createTextMessage(msg);
        }
      });
    }
   
  /**
   * 默认队列发送消息
   */
    public void sendMessage(final String msg) {
      String destination =  jmsQueueTemplate.getDefaultDestination().toString();
      System.out.println("默认队列" +destination+ "发送消息------------" + msg);
      jmsQueueTemplate.send(new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
          return session.createTextMessage(msg);
        }
      });
   
    }
}
